from .skill import Skill
from .equipment import create_equipment
enemy_list = []
frindship_list = []
all_characters = []

class Champion:
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_deck=[], equipment_deck=[], image_path="", level: int = 1 , ai_level = None):
        self.name = name
        self.max_hp = current_hp
        self.current_hp = current_hp
        self.current_mp = current_mp
        self.attack = attack
        self.defence = defence
        self.speed = speed
        self.skills_deck = skills_deck
        self.equipment_deck = equipment_deck
        self.image_path = image_path
        self.level = level
        self.ai_level = ai_level
        self.is_fainted = False

    def take_damage(self, damage):
        self.current_hp = max(0, self.current_hp - damage)
        if self.current_hp <= 0:
            self.is_fainted = True
            return f"{self.name} died!"
        return ""

    def use_skill(self, skill_index,user, enemy):
        if 0 <= skill_index < len(self.skills_deck):
            self.current_mp -= 1
            return self.skills_deck[skill_index].use(user, enemy)
        else:
            return False, "no effect skill"

    def get_hp_percentage(self):
        return float(self.current_hp / self.max_hp) * 100

    def check_eq_num(self):
        if len(self.equipment_desk) > 4:
            raise ValueError("Too many equipments")
        elif len(self.equipment_desk) == 4:
            return "You have already equipped all equipments."
        else:
            return f"You can equip {4 - len(self.equipment_desk)} equipments."

    def check_have_eqed(self, eq_name):
        if eq_name in [eq.name for eq in self.equipment_desk]:
            return True
        else:
            return False
    
    def get_total_atk(self) -> int:
        total = self.attack
        for equip in self.equipment_deck:
            if equip.is_equipped:
                total += equip.attack
        return total

    def get_total_defense(self) -> int:
        total = self.defence
        for equip in self.equipment_deck:
            if equip.is_equipped:
                total += equip.defense
        return total

    def get_total_speed(self) -> int:
        total = self.speed
        for equip in self.equipment_deck:
            if equip.is_equipped:
                total += equip.speed
        return total

    def unequip_same_type(self, equip_type: str):
        for equip in self.equipment_deck:
            if equip.equip_type == equip_type and equip.is_equipped:
                equip.is_equipped = False

    def get_enemy_equip(self, enemy):
        #pylaer get enemy's equipment
        if enemy.equipment_deck:
            for equip in enemy.equipment_deck:
                #get enemy's equipment that player not have
                if equip not in self.equipment_deck:
                    self.equipment_deck.append(equip)
        #enemy out enemy's equipment
        for equip in enemy.equipment_deck:
            enemy.equipment_deck.remove(equip)

    def check_level(self, level_num):
        if self.level < level_num:
            return False
        else:
            return True
        
    def check_max_level(self):
        if self.level == 6:
            return True
        else:
            return False
    def __str__(self):
        return (f"Champion Name: {self.name}\n"
                f"HP: {self.current_hp}\n"
                f"MP: {self.current_mp}\n"
                f"Attack: {self.attack}\n"
                f"Defence: {self.defence}\n"
                f"Speed: {self.speed}\n"
                f"Skills: {self.skills_tech}\n"
                f"Equipment: {self.equipment_deck}")
    
class CharacterWater(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "water"

class CharacterFire(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "fire"

class CharacterIce(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "ice"

class CharacterLightning(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "lightning"

class CharacterWind(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "wind"

class CharacterDark(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "dark"

class CharacterLight(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "light"

class CharacterGrass(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "grass"

class CharacterStone(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "stone"

class CharacterSky(Champion):
    def __init__(self, name: str, current_hp: float, current_mp: int, attack: float, defence: float, speed: float, skills_tech=[], equipment_deck=[], img_path="", level: int = 1 , ai_level = None):
        super().__init__(name, current_hp, current_mp, attack, defence, speed, skills_tech, equipment_deck, img_path, level, ai_level)
        self.attribute = "sky"

# 实例化
all_attributes = ["water", "fire", "ice", "lightning", "wind", "dark", "light", "grass", "stone", "sky"]

def create_skill():
    return {
        "skill1": Skill("Water Surge", all_attributes[0], 70, 95, 20, 15),
        "skill2": Skill("Fire Blaze", all_attributes[1], 80, 90, 15, 20),
        "skill3": Skill("Ice Shard", all_attributes[2], 60, 95, 25, 12),
        "skill4": Skill("Lightning Strike", all_attributes[3], 65, 90, 22, 16),
        "skill5": Skill("Wind Gust", all_attributes[4], 75, 85, 18, 18),
        "skill6": Skill("Dark Slash", all_attributes[5], 75, 90, 16, 20),
        "skill7": Skill("Light Heal", all_attributes[6], 0, 100, 10, 30),
        "skill8": Skill("Grass Blade", all_attributes[7], 75, 85, 18, 18),
        "skill9": Skill("Stone Smash", all_attributes[8], 85, 80, 12, 25),
    }

equipment_data = create_equipment()

def create_player():
    skill_data = create_skill()
    # 每个角色使用与自身属性匹配的技能，并补充克制属性的技能
    return {
        "Libai": CharacterWater(
            "Libai", 230, 7000, 7300, 650, 620, 
            [skill_data["skill1"], skill_data["skill9"], skill_data["skill8"],skill_data["skill3"] ], # 水属性技能+克制石和草的技能
            [equipment_data["sword"]], "data/image/libai.png"
        ),
        "Luyou": CharacterFire(
            "Luyou", 140, 5500, 5000, 500, 480, 
            [skill_data["skill2"], skill_data["skill3"], skill_data["skill6"], skill_data["skill1"]],
            [equipment_data["sword"]], "data/image/luyou.png"
        ),
        "Wangwei": CharacterIce(
            "Wangwei", 130, 4800, 6000, 450, 520, 
            [skill_data["skill3"], skill_data["skill1"], skill_data["skill5"],  skill_data["skill4"]],
            [equipment_data["sword"]], "data/image/wangwei.png"
        ),
        "Baijuyi": CharacterLightning(
            "Baijuyi", 190, 5200, 4500, 600, 600, 
            [skill_data["skill4"], skill_data["skill1"], skill_data["skill6"],  skill_data["skill5"]],
            [equipment_data["sword"]], "data/image/baijuyi.png"
        ),
        "Sushi": CharacterWind(
            "Sushi", 170, 5000, 520, 5000, 6200, 
            [skill_data["skill5"], skill_data["skill2"], skill_data["skill7"], skill_data["skill6"]],
            [equipment_data["sword"]], "data/image/sushi.png"
        ),
    }

def create_enemy():
    skill_data = create_skill()
    return {
        1: CharacterDark(
            "Liqingzhao", 140, 4500, 4200, 300, 380, 
            [skill_data["skill6"], skill_data["skill4"], skill_data["skill5"]], 
            [equipment_data["shield"]], "data/image/liqingzhao.png", 1, "easy_ai"
        ),
        2: CharacterLight(
            "Ouyang", 160, 4600, 4600, 320, 430, 
            [skill_data["skill7"], skill_data["skill6"], skill_data["skill3"]], 
            [equipment_data["HP injection"]], "data/image/ouyang.png", 2, "easy_ai"
        ),
        3: CharacterGrass(
            "Dumu", 170, 4700, 5200, 400, 480, 
            [skill_data["skill8"], skill_data["skill1"], skill_data["skill4"]], 
            [equipment_data["magic ring"]], "data/image/dumu.png", 3, "medium_ai"
        ),
        4: CharacterStone(
            "Yuanmei", 190, 6600, 5000, 480, 360, 
            [skill_data["skill9"], skill_data["skill3"], skill_data["skill5"]],  
            [equipment_data["potion"]], "data/image/yuanmei.png", 4, "medium_ai"
        ),
        5: CharacterSky(
            "Lihe", 220, 6800, 6200, 460, 550, 
            [skill_data["skill9"], skill_data["skill1"], skill_data["skill2"]], 
            [equipment_data["potion"]], "data/image/lihe.png", 5, "hard_ai"
        )
    }
#test
# player_data = create_player()
# enemy_data = create_enemy()
# player1 = player_data["Libai"]
# enemy1 = enemy_data[1]
# print(player1.equipment_deck)
# player1.get_enemy_equip(enemy1)
# print(player1.equipment_deck)
